
using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;



namespace ProjectNocturnalFool
{
    /// <summary>
    /// Sample showing how to manage different game states, with transitions
    /// between menu screens, a loading screen, the game itself, and a pause
    /// menu. This main game class is extremely simple: all the interesting
    /// stuff happens in the ScreenManager component.
    /// </summary>
    public sealed class TacticsGame : Microsoft.Xna.Framework.Game
    {


        #region Fields

        public SpriteFont gameFont;

        public GraphicsDeviceManager graphics;

        public SpriteBatch spriteBatch;
        


        public string message = "";
        public int messageSent = 0;

        // By preloading any assets used by UI rendering, we avoid framerate glitches
        // when they suddenly need to be loaded in the middle of a menu transition.
        static readonly string[] preloadAssets =
        {
            
        };

        #endregion

        #region Initialization

        /// <summary>
        /// Allocate ourselves.
        /// We have a private constructor, so no one else can.
        /// </summary>
        static readonly TacticsGame _instance = new TacticsGame();

        /// <summary>
        /// Access SiteStructure.Instance to get the singleton object.
        /// Then call methods on that instance.
        /// </summary>
        public static TacticsGame Instance
        {
            get { return _instance; }
        }

        /// <summary>
        /// The main game constructor.
        /// </summary>
        public TacticsGame()
        {
            Content.RootDirectory = "Content";
            
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = Constants.WindowWidth;
            graphics.PreferredBackBufferHeight = Constants.WindowHeight;
            graphics.IsFullScreen = Constants.FullScreen;

            Window.ClientSizeChanged += new EventHandler<EventArgs>(Window_ClientSizeChanged);
            Window.AllowUserResizing = true;

        }


        /// <summary>
        /// Loads graphics content.
        /// </summary>
        protected override void LoadContent()
        {
            foreach (string asset in preloadAssets)
            {
                Content.Load<object>(asset);
            }
                        //Load additional Content

            gameFont = Content.Load<SpriteFont>(Constants.Font);
            VirtualScreen.Instance.initialize(Constants.VirtualWidth, Constants.VirtualHeight, GraphicsDevice);
            spriteBatch = new SpriteBatch(GraphicsDevice);
            ScreenMgr.Instance.LoadContent();
            MouseImageMgr.Instance.LoadContent();
            DebugMgr.Instance.LoadContent();
            NetworkMgr.Instance.InitializeSinglePlayer();

            GameLogic.Instance.gameCharacterSession.InitializeSession(SessionType.CampaignRegular);
            ScreenMgr.Instance.OpenScreen(Constants.gameState);

            XMLMgr.getAbilityList(AbilityAttribute.Arcane);
            //XMLMgr.editAbilityXML();
        }

        protected override void Update(GameTime gameTime)
        {
            InputMgr.Instance.Update();
            GameLogic.Instance.Update(gameTime);
            ScreenMgr.Instance.Update(gameTime);
            VirtualScreen.Instance.Update();
            base.Update(gameTime);
            MouseImageMgr.Instance.Update(gameTime);
            NetworkMgr.Instance.Update(gameTime);

            //Clear any inputs from this update
            InputMgr.Instance.KeyboardBuffer.Clear();

            if(InputMgr.Instance.IsButtonPress(PlayerIndex.One,Input.Reject))
                this.Exit();            
       }
        
 


        protected override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            ScreenMgr.Instance.Draw(gameTime);
            MouseImageMgr.Instance.Draw(gameTime);
        }


        #endregion


        void Window_ClientSizeChanged(object sender, EventArgs e)
        {
            VirtualScreen.Instance.PhysicalResolutionChanged();
        }

        protected override void OnExiting(Object sender, EventArgs args)
        {

            NetworkMgr.Instance.server.Shutdown("Program Exiting");

            NetworkMgr.Instance.client.Disconnect("Program Exiting");

            Thread.Sleep(500); 

            base.OnExiting(sender, args);

            // Stop the threads
        }
    }


    #region Entry Point

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static class Program
    {
        static void Main()
        {
            TacticsGame.Instance.Run();
        }
    }

    #endregion
}
